package com.longtop.perform.zb.index.service;

import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.util.StringUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.perform.zb.index.model.TZbIndex;

/**
 * 指标管理--复合指标Service
 * 
 * @author 薛延明
 * 
 */
public class IndexCustomService extends BaseService {

	public static final String BEAN_ID = "indexCustomService";
	//  指标类型（复合指标TYPE=3）
	public static final String INDEX_TYPE = "3";

	@SuppressWarnings("unchecked")
	public QueryResult searchIndexCustomList(Map<String, String> params, int pageNo, int pageSize) throws BaseCheckedException {

		String indexId = params.get("indexId");
		String indexName = params.get("indexName");
		// 查询条件辅助类
		QueryConditions queryConditions = WebUtil.createQueryConditions();
		if (!"".equals(indexId)) {
			queryConditions.setCondition("indexId", QueryConditions.INCLUDE, indexId);
		}
		if (!"".equals(indexName)) {
			queryConditions.setCondition("indexName", QueryConditions.INCLUDE, indexName);
		}
		//queryConditions.setCondition("indexName", QueryConditions.EQUAL, INDEX_TYPE);

		Object[] values = null;
		String[] propertyNames = StringUtil.listToStrArray(queryConditions.getPropertyNames());
		String[] operators = StringUtil.listToStrArray(queryConditions.getOperators());
		List valuesList = queryConditions.getValues();
		if (valuesList != null || valuesList.size() > 0) {
			values = valuesList.toArray();
		}
		// 当前开始页数
		int offset = (pageNo - 1) * pageSize;
		// 执行HQL查询
		Object[] result = this.query("from ZbIndex", propertyNames, operators, values, offset, pageSize, true, "updateTime desc", null, null);
		return new QueryResult((List) result[0], result[1] != null ? ((Integer) result[1]).intValue() : 0);
	}

	/**
	 * 根据指标ID查询指标
	 * 
	 * @param indexId
	 * @return
	 */
	public TZbIndex findZbIndexInfoByIndexId(String indexId) throws BaseCheckedException {
		
		return this.get(TZbIndex.class, indexId);
	}

	/**
	 * 保存指标信息
	 * 
	 * @param zbIndex
	 */
	public void saveIndexCustomInfo(TZbIndex zbIndex) throws BaseCheckedException {
		
		this.save(zbIndex);
	}

	/**
	 * 修改指标信息
	 * 
	 * @param zbIndex
	 */
	public void updateIndexCustomInfo(TZbIndex zbIndex) throws BaseCheckedException {
		
		this.update(zbIndex);
	}

	/**
	 * 删除复合指标
	 * 
	 * @param indexId 指标ID
	 * @throws BaseCheckedException
	 */
	public void deleteIndexCustom(String indexId) throws BaseCheckedException {

		this.delete(TZbIndex.class, indexId);
	}
}